﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.ComponentModel;

namespace SAF.Framework.Controls.Upgrade
{
    internal class DownloadFile : INotifyPropertyChanged
    {
        private string m_Path = string.Empty;
        private string m_Name = string.Empty;
        private string m_Version = string.Empty;
        private string m_State = string.Empty;
        private long m_Size = 0L;

        /// <summary>
        /// 文件下载路径
        /// </summary>
        public string Path
        {
            get { return this.m_Path; }
            set { this.m_Path = value; }
        }
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName
        {
            get { return System.IO.Path.GetFileName(this.m_Path); }
        }
        /// <summary>
        /// 最新版本号
        /// </summary>
        public string Version
        {
            get { return this.m_Version; }
            set { this.m_Version = value; }
        }
        /// <summary>
        /// 更新状态
        /// </summary>
        public string State
        {
            get { return m_State; }
            set
            {
                m_State = value;
                Notify("State");
            }
        }
        /// <summary>
        /// 文件大小,单位:Byte
        /// </summary>
        public long Size
        {
            get { return m_Size; }
            set { m_Size = value; }
        }
        /// <summary>
        /// 文件大小,单位:M
        /// </summary>
        public string ConversionSize
        {
            get
            {
                if (this.Size > 1024 * 1024 * 1024)
                {
                    return (this.Size / (1024 * 1024 * 1024.0)).ToString("0.#") + " GB";
                }
                else if (this.Size > 1024 * 1024)
                {
                    return (this.Size / (1024 * 1024.0)).ToString("0.#") + " MB";
                }
                else if ((this.Size > 1024))
                {
                    return (this.Size / 1024.0).ToString("0.#") + " KB";
                }
                else
                {
                    return this.Size.ToString() + " B";
                }
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="file"></param>
        public DownloadFile(UpgradeConfigFile file)
        {
            this.m_Path = file.path;
            this.m_Version = file.version;
            this.m_Size = Helper.GetHTTPFileSize(file.path);
        }

        void Notify(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
